package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.*;
import com.dongdongshop.result.Result;
import com.dongdongshop.result.ResultEnum;
import com.dongdongshop.service.AddressService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@RequestMapping("address")
@Controller
public class AddressController {

    private static final Logger logger = LoggerFactory.getLogger(AddressController.class);

    @Reference
    private AddressService addressService;

    @RequestMapping("toAddressShow")
    public String toAddressShow(Model model){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (null != user){
            model.addAttribute("tbUser",user.getUsername());
        }
        List<TbProvinces> provincesList = addressService.getProvincesList();
        model.addAttribute("provincesList",provincesList);
        return "home-setting-address";
    }

    @RequestMapping("getAddressListByUser")
    @ResponseBody
    public Result getAddressListByUser(){
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        try {
            List<TbAddress> addressList = addressService.getAddressListByUser(tbUser.getUsername());
            for (TbAddress tbAddress : addressList) {
                String province = addressService.getProvinceById(tbAddress.getProvinceId());
                tbAddress.setProvince(province);
                String city = addressService.getCityById(tbAddress.getCityId());
                tbAddress.setCity(city);
                String town = addressService.getAreaById(tbAddress.getTownId());
                tbAddress.setTown(town);
            }
            return Result.jsonResponse(ResultEnum.OK).setData(addressList);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("listCityByProvinceId")
    @ResponseBody
    public Result listCityByProvinceId(String provinceId) {
        try {
            List<TbCities> citiesList = addressService.listCityByProvinceId(provinceId);
            return Result.jsonResponse(ResultEnum.OK).setData(citiesList);
        } catch (Exception e) {
            logger.error("系统错误", e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("listAreaByCityId")
    @ResponseBody
    public Result listAreaByCityId(String cityId) {
        try {
            List<TbAreas> areas = addressService.listAreaByCityId(cityId);
            return Result.jsonResponse(ResultEnum.OK).setData(areas);
        } catch (Exception e) {
            logger.error("系统错误", e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("insertAddress")
    @ResponseBody
    public Result insertAddress(TbAddress tbAddress) {
        // 获取当前用户
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        try {
            tbAddress.setUserId(tbUser.getUsername());
            tbAddress.setIsDefault("1");
            tbAddress.setCreateDate(new Date());
            int i = addressService.insertAddress(tbAddress);
            return Result.jsonResponse(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("系统错误", e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateDefaultAddress")
    @ResponseBody
    public Result updateDefaultAddress(Long id) {
        // 当前登录的用户
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        try {
            int i = addressService.updateDefaultAddress(tbUser.getUsername(), id);
            return Result.jsonResponse(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("系统错误", e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }


    @RequestMapping("toUpdateAddress")
    @ResponseBody
    public Result toUpdateAddress(Long id) {
        try {
            TbAddress tbAddress = addressService.getAddressById(id);
            return Result.jsonResponse(ResultEnum.OK).setData(tbAddress);
        } catch (Exception e) {
            logger.error("系统错误", e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateAddress")
    @ResponseBody
    public Result updateAddress(TbAddress tbAddress) {
        try {
            int i = addressService.updateAddress(tbAddress);
            return Result.jsonResponse(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("系统错误", e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("deleteAddress")
    @ResponseBody
    public Result deleteAddress(Long id) {
        try {
            int i = addressService.deleteAddress(id);
            return Result.jsonResponse(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("系统错误", e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

}
